<?php

class Port_controller extends CI_Controller {

    function __construct(){
        parent::__construct();
        $this->load->library('session');
        $this->load->helper('url');
        $this->load->helper('utils');
                            
        Validator::is_access($this->session->userdata('user_id'));
    }

    function index(){

        $section = "port";
        $title = _LB_PORT;
        $cfer = new Cfer(array(
        $title => base_url('port')));
        
        $filter = array();
        $filter['name'] = $this->input->post('name');
        $filter['country'] = $this->input->post('country');
        $port = Port::getList($filter);

        $this->data['port'] = $port;
        $this->data['filter'] = $filter;
        $this->data['title'] = $title;
        

        $array_menus = array();
        $country = Country::getList(array('disabled'=>'0'));
        
        $filter = array();
        $filter['parent_id'] = 0;
        $filter['type'] = 1;
        Menu::getMenuList($array_menus, $filter);

        $this->data['array_menus'] = $array_menus;
        $this->data['section'] = $section;
        $this->data['country'] = $country;
        $this->data['cfer'] = $cfer;
        $this->load->view('port_list', $this->data);
    }

    function delete($id = null) {
        $port = new Port();

        if ($id && !$port->get($id)) {
                redirect(base_url('port'));
        }
        $port->delete();
        redirect(base_url('port'));
    }
    
    function form($id = null) {
        $section = "port";
		
        if ($id) {
            $cfer = new Cfer(array(
            _LB_PORT => 'port',
            _LB_EDIT_PORT => base_url('port/form/'.$id)));
        } else {
            $cfer = new Cfer(array(
            _LB_PORT => 'port',
            _LB_ADD_PORT => base_url('port/form/')));
        }
        
        $act = $this->input->get_post('act');
    
        $port = new Port();

        if ($id && !$port->get($id)) {
                redirect(base_url('port'));
        }
        
        if ($act == ACT_VALID_EDIT) {

      
            $port->id_wp_country = $this->input->post('country');
            $port->name = $this->input->post('name');
            if ($port->validateInput()) {
                if ($id) {
                    $port->update();
                } else {
                    $port->insert();
                }
                redirect(base_url('port'));
            }
        }

        $country = Country::getList(array('disabled'=>'0'));
        
		$array_menus = array();
        $filter = array();
        $filter['parent_id'] = 0;
        $filter['type'] = 1;
        Menu::getMenuList($array_menus, $filter);
        
        $title = _LB_PORT;
        
        $this->data['port'] = $port;
        $this->data['title'] = $title;

        $this->data['array_menus'] = $array_menus;
        $this->data['backlink'] = base_url('port');
        $this->data['section'] = $section;
        $this->data['country'] = $country;
        
        $this->data['cfer'] = $cfer;
        $this->load->view('port_form', $this->data);
    }
}
